package com.example.month11.vp_tab.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.recyclerview01.R;
import com.example.utils.L;


public class SplashFragment extends Fragment {
    public static final String BUNDLE_KEY_RES_ID = "key_res_id";

    private ImageView mIvContent;
    private int mResId;

    // 静态工厂方法。便于使用setArguments，避免页面销毁，丢失Arguments数据。
    public static SplashFragment newInstance(int resId) {
        Bundle bundle = new Bundle();
        bundle.putInt(BUNDLE_KEY_RES_ID, resId);
        SplashFragment fragment = new SplashFragment();
        fragment.setArguments(bundle); // 传递参数给Fragment。系统重建的时候。Arguments中的数据，还能恢复。
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();  // 获取Activity传递的数据
        if (arguments != null) {
            mResId = arguments.getInt(BUNDLE_KEY_RES_ID);
        }
        L.d("onCreate", "onCreate:" + mResId);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_splash, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mIvContent = view.findViewById(R.id.iv_content);
        mIvContent.setImageResource(mResId);
        mIvContent.setTag(mResId);
    }
}
